/**
 *  Module
 *
 * Description
 */
angular
  .module('sfhtHaitaoCmsApp')
  .controller('ModuleManageCtrl', function($scope, $q, $modal, $location, $injector, Moduletore) {
    var pageService = $injector.get("PageService");
    $scope.pageType = Moduletore.get("pageType") ? Moduletore.get("pageType") : 0;

    pageService.getModuleList($scope.pageType).then(function(data) {
      $scope.moduleLists = data.value;
    });

    $scope.changeClass = function(type) {
      $scope.pageType = type;
      Moduletore.set("pageType", type);
      pageService.getModuleList(type).then(function(data) {
        $scope.moduleLists = data.value;
      });
    }

    $scope.moduleEdit = function(module) {
      $location.path('/module/manager/' + module.id);
    }

    var empty = function() {};

    //模块属性
    $scope.moduleDataInfo = function(module, $event, isEdit) {
      $event.preventDefault();
      if (isEdit) {
        pageService.getModuleDetail(module.moduleName, module.moduleType, true).then(function(data) {
          module = data;
          var modal = $modal.open({
            animation: true,
            templateUrl: 'modal.module.info.html',
            controller: 'ModalEditModuleInfoCtrl',
            size: 'lg',
            resolve: {
              module: function() {
                return module;
              },
              isEdit: function() {
                return isEdit;
              }
            }
          });
          modal.result.then(empty, empty);
        });
      } else {
        var modal = $modal.open({
          animation: true,
          templateUrl: 'modal.module.info.html',
          controller: 'ModalEditModuleInfoCtrl',
          size: 'lg',
          resolve: {
            module: function() {
              return module;
            },
            isEdit: function() {
              return isEdit;
            }
          }
        });
        modal.result.then(empty, empty);
      }
    };
  })
  .controller('ModalEditModuleInfoCtrl', function($scope, $route, $q, $injector, $modal, $modalInstance, module, isEdit, Uploader, Moduletore) {
    $scope.module = module || {};
    $scope.module.moduleType = !isEdit ? Moduletore.get("pageType") : 0;
    $scope.isEdit = isEdit;
    var commonBizService = $injector.get("CommonBiz");

    function getPicUrl(slide){
      var defer = $q.defer();
      Uploader.uploadFile($scope, slide, "modulePic");
      defer.resolve(slide);
      return defer.promise;
    }
    $scope.changeFile = function(slide) {
      getPicUrl(slide).then(function(data){
        console.log(data.picUrl);
      })

    };

    /**
     * 编辑模块
     * @param  {object} 模块内容
     * @return {array}
     */
    $scope.save = function(module) {
      commonBizService.updateModuleDraft(module)
        .then(function(data) {
          alert("编辑成功！！");
          $modalInstance.close(data);
        }, function(data) {
          alert("编辑失败，" + data);
        });
    };

    /**
     * 添加新模块
     * @param  {object} 新模块内容
     * @return {array}
     */
    $scope.add = function(newModule) {
      commonBizService.createModuleDraft(newModule)
        .then(function(data) {
          if (data.value) {
            Moduletore.set("pageType", newModule.moduleType);
            $modalInstance.close(data);
            $route.reload();
            // window.location.reload();
          } else {
            alert("编辑失败，" + data);
          }
        }, function(data) {
          alert("编辑失败，" + data);
        })
    };

    $scope.cancel = function() {
      $modalInstance.dismiss("cancel");
    };
  });